import { WebSocket } from "ws";
import { User } from "../../../types/User";
import { roomsTable } from "../../../database/rooms-table";
import { conversionMessage } from "../helpers/message-converter";
import { Room } from "../../../types/Room";
import { createId } from "../../../helpers/id-creater";
import { CreateRoomMessageData } from "../types/MessageData";

export const createRoomHandler = ({ user, payload, client }: { client: WebSocket; user: User; payload: CreateRoomMessageData["payload"] }) => {
	const room: Room = {
		roomId: createId(),
		name: payload.name,
		players: [
			{
				user,
				dead: false,
				heros: [],
				currentHerosSkinId: [],
				hiddenHeroIds: [],
				healthLimit: 0,
				health: 0,
				skills: [],
				handCards: [],
				materielCards: [],
				delayedCards: [],
				buckleCards: [],
				marks: [],
				tied: false,
				flipped: false,
				client,
			},
		],
		heros: [],
		drawCards: [],
		discardCards: [],
		logs: [],
	};
	roomsTable.rooms.push(room);
	client.send(conversionMessage({ type: "createdRoom", payload: { roomId: room.roomId } }));
	const log = { userId: user.userId, username: user.username, log: "创建了房间。" };
	room.logs.push(log);
	client.send(conversionMessage({ type: "roomLog", payload: log }));
};
